<?php
include "./inc/header.php";

class Animal
{
	var $name;

	public function hi()
	{
		echo 'hi, ', $this->name, "\n";
	}

	public static function ho()
	{
		// 单引号内的特殊序列不会被转义 eg: '\n'
		echo 'ho func called', "\n";
	}

	function move()
	{
		echo "animal is running";
	}
}

title('面向对象');


comment('访问属性和方法');
$dog = new Animal();
$dog->name = 'husky';
$dog->hi();

comment('可添加任意属性');
$dog->like = 'running';
clog($dog->like);

$prop = 'name';
$arr = array('name', 'like');

function getProp()
{
	global $arr;
	return $arr[1];
}


comment('访问动态属性(属性由变量或方法返回值决定)');
clog(getProp() . '<==');
clog($dog->$prop);
clog($dog->{$prop});
clog($dog->{$arr[1]});
clog($dog->{getProp()});

comment('打印声明的类');
print_r(get_declared_classes());
//var_dump(Animal); // error
//print_r(Animal);
comment('打印类的方法');
print_r(get_class_methods($dog));

comment('打印对象');
print_r($dog);


comment('访问静态方法');
Animal::ho();

comment('查看对象有哪些属性 (即自省)');
print_r(get_object_vars($dog));

comment('三元运算符 和 自动类型转换');
clog('1' == 1 ? 'yes' : 'n');
clog('1' == 10 ? 'y' : 'n');

include 'inc/footer.php';
